package cn.itsource.test.jsoup;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.URL;

/**
 * @author : 张金铭
 * @description :
 * @create :2023-03-06 09:03:00
 */
public class GatJdDemo {
    public static void main(String[] args) {
//        test();
//        getAllText();
//        getHide();
//        jdNew();
//        getFC();
        getTc();
    }


    public static void test(){
        String url = "https://www.jd.com/";

        try {
            Document jd = Jsoup.parse(new URL(url), 5000);
            Element navitems = jd.getElementById("navitems");
            Elements ul = navitems.getElementsByTag("ul");
            for (Element element : ul) {
                Elements lis = element.getElementsByTag("li");
                for (Element li : lis) {
                    String a = li.getElementsByTag("a").get(0).html();
                    System.out.println(a);
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getAllText(){
        String url = "https://www.jd.com/";
        try {
            Document jd = Jsoup.parse(new URL(url), 5000);
            Element navitems = jd.getElementById("J_cate");
            Elements ul = navitems.getElementsByTag("ul");
            for (Element element : ul) {
                Elements lis = element.getElementsByTag("li");
                for (Element li : lis) {
                    String a = li.getElementsByTag("a").get(0).attr("href");
                    String html = li.getElementsByTag("a").get(0).html();
                    System.out.print(html+": ");
                    System.out.println("https:"+a);
                }
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getHide(){
        String url = "https://www.jd.com/";
        String cssQuery = ".cate_channel a";
        try {
            Document jd = Jsoup.parse(new URL(url), 10000);
            Elements lazyimg_ = jd.getElementsByClass("lazyimg");
            System.out.println(lazyimg_.first().getElementsByTag("img").get(0).attr("href"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 这段代码定义了一个名为jdNew的静态方法，
     * 该方法中使用Jsoup库向京东的网站主页发起一个HTTP GET请求，
     * 获取页面内容，并使用Jsoup提供的方法对页面内容进行解析和查询，
     * 最终找到ID为“J_news”的元素，并进一步查找其中的子元素div和class为“news_list”的元素。
     * 然后在这个class为“news_list”的元素下遍历每个li元素，
     * 获取其中的第一个a元素的span和html属性，最后将其打印出来。
     * 这样就实现了获取京东主页中class为“news_list”的标签下所有a标签的span和html内容。
     *
     */
    public static void jdNew(){
        String url = "https://www.jd.com/";

        try {
            Document document = Jsoup.parse(new URL(url), 5000);
            Element j_news = document.getElementById("J_news");
            Elements div = j_news.getElementsByTag("div");
            Elements uls = div.first().getElementsByClass("news_list");
            for (Element li : uls.first().getElementsByTag("li")) {
                Elements a = li.getElementsByTag("a");
                String span = a.first().getElementsByTag("span").html();
                String html = a.first().html();
                System.out.println(span+":"+html);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getFC(){
        String url = "http://kaijiang.500.com/?0_ala_baidu";
        String id = "sfc";
        try {
            Document parse = Jsoup.parse(new URL(url), 5000);
            Element sfcTr = parse.getElementById(id);
            Elements td = sfcTr.getElementsByTag("td");
            for (Element element : td) {
                System.out.println(element.html());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getTc(){
        //https://kaijiang.500.com/shtml/sfc/23022.shtml
        for (int extendPro = 23022;extendPro<=23030;extendPro++){
            String url = "http://kaijiang.500.com/shtml/sfc/"+extendPro+".shtml";
            try {
                Document parse = Jsoup.parse(new URL(url), 5000);
                Elements kj_tablelist02 = parse.getElementsByClass("kj_tablelist02");
                Element element = kj_tablelist02.first().getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(2);
                Element extend = kj_tablelist02.first().getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(0);
                String html = extend.getElementsByTag("td").get(0)
                        .getElementsByTag("span").get(0)
                        .getElementsByTag("a").get(0)
                        .getElementsByTag("font").get(0)
                        .getElementsByTag("strong").get(0).html();
                System.out.println("第"+html+"期");
                for (Element td : element.getElementsByTag("td")) {
                    System.out.print(td.getElementsByTag("span").get(0).html());
                }
                System.out.println();
                //获取中奖奖金
                String moneyOne = kj_tablelist02.get(1).getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(2)
                        .getElementsByTag("td").get(2).html();
                String moneyTwo = kj_tablelist02.get(1).getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(3)
                        .getElementsByTag("td").get(2).html();
                String moneyThree = kj_tablelist02.get(1).getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(4)
                        .getElementsByTag("td").get(2).html();

                //获取中奖注数
                String moneyToOne = kj_tablelist02.get(1).getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(2)
                        .getElementsByTag("td").get(1).html();
                String moneyToTwo = kj_tablelist02.get(1).getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(3)
                        .getElementsByTag("td").get(1).html();
                String moneyToThree = kj_tablelist02.get(1).getElementsByTag("tbody").get(0)
                        .getElementsByTag("tr").get(4)
                        .getElementsByTag("td").get(1).html();



                System.out.println("一等奖单注奖金："+moneyOne+"中奖注数"+moneyToOne);
                System.out.println("二等奖单注奖金："+moneyTwo+"中奖注数"+moneyToTwo);
                System.out.println("三等奖单注奖金："+moneyThree+"中奖注数"+moneyToThree);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
